package com.samsung.service.impl.lots;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.samsung.common.util.BaseUtil;
import com.samsung.dao.lots.LotsCandidateMapper;
import com.samsung.dao.lots.LotsEventMapper;
import com.samsung.dao.lots.LotsItemMapper;
import com.samsung.dao.lots.LotsRecordMapper;
import com.samsung.model.lots.LotsCandidate;
import com.samsung.model.lots.LotsEvent;
import com.samsung.model.lots.LotsItem;
import com.samsung.model.lots.LotsRecord;
import com.samsung.service.lots.LotsService;


/** 
 * @ClassName: LotsServiceImpl 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author hao1.yan 
 * @date 2015年5月14日 上午9:18:11  
 */
@Service("lotsService")
public  class LotsServiceImpl implements LotsService{

	/* (non-Javadoc)
	 * @see com.samsung.service.lots.LotsService#addLots(com.samsung.model.lots.LotsEvent)
	 */
	@Resource
	private LotsEventMapper eventMapper;
	@Resource
	private LotsRecordMapper recordMapper;
	@Resource
	private LotsCandidateMapper candidateMapper;
	@Resource
	private LotsItemMapper itemMapper;
	
	@Override
	public int insetLotsRecord(LotsRecord record, String lid) {
		LotsEvent lotsEvent=eventMapper.selectByPrimaryKey(lid);
		if(lotsEvent!=null){
			record.setCreateTime(lotsEvent.getCreateTime());
			record.setId(BaseUtil.getUUID());
			record.setLid(lid);
			record.setIid(lotsEvent.getUid());
			record.setStartTime(new Date());
			recordMapper.insert(record);
			return 1;
		}
		return 0;
	}
	
	@Override
	@Transactional
	public int addLots(LotsEvent lotsEvent,String candatesId,String itemsTitle) {
		lotsEvent.setId(BaseUtil.getUUID());
		eventMapper.insert(lotsEvent);
		String[] cadidates=candatesId.split(",");
		//加入抽签参与人员
		for (String id : cadidates) {
			LotsCandidate candidate=new LotsCandidate();
			candidate.setId(BaseUtil.getUUID());
			candidate.setLid(lotsEvent.getId());
			if(!id.equals("")){
				candidate.setUid(id);
				candidateMapper.insert(candidate);
			}
		}
		//加入分组抽签的分组信息
		if(itemsTitle!=""){
			String[] items=itemsTitle.split(",");
			for (String title : items) {
				LotsItem item=new LotsItem();
				item.setId(BaseUtil.getUUID());
				item.setLid(lotsEvent.getId());
				item.setTitle(title);
				itemMapper.insert(item);
			}
		}
		return 1;
	}
	
	/* (non-Javadoc)
	 * @see com.samsung.service.lots.LotsService#selectLotsEventByPrimaryKey(java.lang.String)
	 */
	@Override
	public LotsEvent selectLotsEventByPrimaryKey(String id) {
		return eventMapper.selectByPrimaryKey(id);
	}
	/**
	 * 
	 * @author ziyan1.zhao
	 * */
	@Override
	public List<LotsEvent> listNewLots(int max) {
		return eventMapper.listNewLots(max);
	}

	@Override
	public int getRecordCount(String lotsId) {
		return recordMapper.getRecordCount(lotsId);
	}

	/* (non-Javadoc)
	 * @see com.samsung.service.lots.LotsService#listLotsItem(java.lang.String)
	 */
	@Override
	public List<LotsItem> listLotsItem(String lid) {
		return itemMapper.selectLotsItemByLid(lid);
}
	@Override
	public List<LotsCandidate> getCandidate(String lid) {
		return candidateMapper.selectLotsCandidateByLid(lid);
	}
	
	@Override
	public int insetLotsRecord(LotsRecord record) {
		
		return recordMapper.insert(record);
	}

	@Override
	public int saveLotsRecord(LotsRecord lt) {
		return recordMapper.insert(lt);
	}

	/* (non-Javadoc)
	 * @see com.samsung.service.lots.LotsService#getPageInfo(int, int)
	 */
	@Override
	public List<LotsEvent> getPageInfo(int startPos, int pageSize) {
		Map<String, Integer> paramMap = new HashMap<String, Integer>();
		paramMap.put("startPos", startPos);
		paramMap.put("pageSize", pageSize);
		List<LotsEvent> list=eventMapper.getPageInfo(paramMap);
		return list;
	}

	/* (non-Javadoc)
	 * @see com.samsung.service.lots.LotsService#getLotsRecord(java.lang.String)
	 */
	@Override
	public LotsRecord getLotsRecord(String lid) {
		return recordMapper.selectByPrimaryKey(lid);
	}

	/* (non-Javadoc)
	 * @see com.samsung.service.lots.LotsService#getLotsRecordByLid(java.lang.String)
	 */
	@Override
	public List<LotsRecord> getLotsRecordByLid(String lid) {
		return recordMapper.getLotsRecordByLid(lid);
	}

	/* (non-Javadoc)
	 * @see com.samsung.service.lots.LotsService#selectByPrimaryKey(java.lang.String)
	 */
	@Override
	public LotsItem selectByPrimaryKey(String id) {
		return itemMapper.selectByPrimaryKey(id);
	}

	@Override
	public int delete(String lid) {
		recordMapper.deleteByLid(lid);
		candidateMapper.deleteByLid(lid);
		itemMapper.deleteByLid(lid);
		return eventMapper.deleteByPrimaryKey(lid);
	}

	@Override
	public int getTotalCount(Map<String, Object> condition) {
		return eventMapper.getTotalCount( condition);
	}

	@Override
	public List<Map<String, Object>> getLotsList(Map<String, Object> condition) {
		return eventMapper.getLotsList( condition);
	}
}
